﻿/*
 * ========================================================================
 * Copyright(c) 2008-2010北京高阳金信信息技术有限公司, All Rights Reserved.
 * ========================================================================
 *  客户管理--基本信息(查询对话框)
 * 
 * 作者：张生     时间：2010年6月9日11:21:57
 * 版本：V1.0
 * 
 * 修改者：         时间：                
 * 修改说明：
 * ========================================================================
*/
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Common;
using agTweener;
using System.Windows.Threading;
using System.Collections.Generic;

namespace SilverlightProject.Dialogs
{
    public class UserInfoDialog : Canvas
    {
        #region 属性
        /// <summary>
        /// 总的父控件Canvas
        /// </summary>
        private Canvas _canvas;
        /// <summary>
        /// 主Panel
        /// </summary>
        private StackPanel pnl_Main;
        /// <summary>
        /// 顶部Panel
        /// </summary>
        private StackPanel pnl_Top;
        /// <summary>
        /// 底部Panel
        /// </summary>
        private StackPanel pnl_Bottom;
        /// <summary>
        /// 主容器Border
        /// </summary>
        private Border _border;
        /// <summary>
        /// 是否关闭查询框
        /// </summary>
        private bool _place = true;
        /// <summary>
        /// 姓名
        /// </summary>
        private TextBox txt_Name;
        /// <summary>
        /// 性别(男)
        /// </summary>
        private RadioButton rbtn_Man;
        /// <summary>
        /// 性别(女)
        /// </summary>
        private RadioButton rbtn_Woman;
        /// <summary>
        /// 性别(全部)
        /// </summary>
        private RadioButton rbtn_All;
        /// <summary>
        /// 手机
        /// </summary>
        private TextBox txt_Phone;
        /// <summary>
        /// 年龄（0）
        /// </summary>
        private TextBox txt_Age0;
        /// <summary>
        /// 年龄（1）
        /// </summary>
        private TextBox txt_Age1;
        /// <summary>
        /// 查询
        /// </summary>
        private Button btn_Serach;
        /// <summary>
        /// 取消按钮(Button)
        /// </summary>
        private Button btn_Cancel;
        /// <summary>
        /// 条件字符串
        /// </summary>
        private string _parameter;

        public event EventHandler LoadData;

        #endregion

        /// <summary>
        /// 父类
        /// </summary>
        private object _containers;
        /// <summary>
        /// 查询面板父控件
        /// </summary>
        public object Containers
        {
            get { return _containers; }
            set { _containers = value; }
        }

        /// <summary>
        /// 构造方法
        /// </summary>
        public UserInfoDialog()
        {
            this.Init();
        }

        /// <summary>
        /// 初始化页面
        /// </summary>
        public void Init()
        {
            this._canvas = new Canvas() { Background = new SolidColorBrush(Colors.White) };// { Name = "canvas", Width = 550, Height = 250 }
            this.Children.Add(this._canvas);

            pnl_Main = new StackPanel() { Orientation = Orientation.Vertical, Width = 360, Height = 248 };
            pnl_Top = new StackPanel() { Width = 360, Height = 220, HorizontalAlignment = HorizontalAlignment.Center, VerticalAlignment = VerticalAlignment.Top };
            pnl_Bottom = new StackPanel() { Width = 360, Height = 28, HorizontalAlignment = HorizontalAlignment.Center, VerticalAlignment = VerticalAlignment.Top };

            Grid _grid = new Grid() { Name = "main_Grid" };
            _grid.ColumnDefinitions.Add(new ColumnDefinition() { Width = GridLength.Auto });
            _grid.ColumnDefinitions.Add(new ColumnDefinition() { Width = GridLength.Auto });
            _grid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(36) });
            _grid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(36) });
            _grid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(36) });
            _grid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(36) });
            _grid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(36) });
            _grid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(36) });

            TextBlock _textBlock = new TextBlock() { Text = "客户管理基本信息查询", FontSize = 22, FontFamily = new FontFamily("黑体"), FontWeight = FontWeights.Bold, HorizontalAlignment = HorizontalAlignment.Center, VerticalAlignment = VerticalAlignment.Center };
            Utils.SetGridColumnSpan(_textBlock, 0, 0, 2);
            _grid.Children.Add(_textBlock);

            _textBlock = new TextBlock() { Text = "姓   名：", FontSize = 15, HorizontalAlignment = HorizontalAlignment.Right, VerticalAlignment = VerticalAlignment.Center };
            Utils.SetGrid(_textBlock, 1, 0);
            _grid.Children.Add(_textBlock);

            this.txt_Name = new TextBox() { Width = 275, Height = 25, FontSize = 15, HorizontalAlignment = HorizontalAlignment.Left, VerticalAlignment = VerticalAlignment.Center };
            Utils.SetGrid(this.txt_Name, 1, 1);
            _grid.Children.Add(this.txt_Name);


            _textBlock = new TextBlock() { Text = "性   别：", FontSize = 15, HorizontalAlignment = HorizontalAlignment.Right, VerticalAlignment = VerticalAlignment.Center };
            Utils.SetGrid(_textBlock, 2, 0);
            _grid.Children.Add(_textBlock);

            StackPanel spnl = new StackPanel() { Orientation = Orientation.Horizontal, HorizontalAlignment = HorizontalAlignment.Left, VerticalAlignment = VerticalAlignment.Center };
            this.rbtn_Man = new RadioButton() { Content = "男", Width = 50, FontSize = 12, HorizontalAlignment = HorizontalAlignment.Left, VerticalAlignment = VerticalAlignment.Center, GroupName = "Sex_Type" };
            spnl.Children.Add(this.rbtn_Man);
            this.rbtn_Woman = new RadioButton() { Content = "女", Width = 50, FontSize = 12, HorizontalAlignment = HorizontalAlignment.Left, VerticalAlignment = VerticalAlignment.Center, GroupName = "Sex_Type" };
            spnl.Children.Add(this.rbtn_Woman);
            this.rbtn_All = new RadioButton() { Content = "全部", Width = 50, IsChecked = true, FontSize = 12, HorizontalAlignment = HorizontalAlignment.Left, VerticalAlignment = VerticalAlignment.Center, GroupName = "Sex_Type" };
            spnl.Children.Add(this.rbtn_All);
            Utils.SetGrid(spnl, 2, 1);
            _grid.Children.Add(spnl);

            _textBlock = new TextBlock() {Text = "手   机：", FontSize = 15, HorizontalAlignment = HorizontalAlignment.Right, VerticalAlignment = VerticalAlignment.Center };
            Utils.SetGrid(_textBlock, 3, 0);
            _grid.Children.Add(_textBlock);

            this.txt_Phone = new TextBox() { Width = 275, Height = 25, FontSize = 15, HorizontalAlignment = HorizontalAlignment.Left, VerticalAlignment = VerticalAlignment.Center };
            Utils.SetGrid(this.txt_Phone, 3, 1);
            _grid.Children.Add(this.txt_Phone);

            _textBlock = new TextBlock() { Text = "年   龄：", FontSize = 15, HorizontalAlignment = HorizontalAlignment.Right, VerticalAlignment = VerticalAlignment.Center };
            Utils.SetGrid(_textBlock, 4, 0);
            _grid.Children.Add(_textBlock);

            spnl = new StackPanel() { Orientation = Orientation.Horizontal, HorizontalAlignment = HorizontalAlignment.Left, VerticalAlignment = VerticalAlignment.Center };
            this.txt_Age0 = new TextBox() { Width = 120,Text="1", Height = 25, FontSize = 15, HorizontalAlignment = HorizontalAlignment.Left, VerticalAlignment = VerticalAlignment.Center };
            spnl.Children.Add(this.txt_Age0);
            _textBlock = new TextBlock() { Text = "----", FontSize = 15, HorizontalAlignment = HorizontalAlignment.Right, VerticalAlignment = VerticalAlignment.Center };
            spnl.Children.Add(_textBlock);
            this.txt_Age1 = new TextBox() { Width = 120, Text = "100", Height = 25, FontSize = 15, HorizontalAlignment = HorizontalAlignment.Left, VerticalAlignment = VerticalAlignment.Center };
            spnl.Children.Add(this.txt_Age1);
            Utils.SetGrid(spnl, 4, 1);
            _grid.Children.Add(spnl);

            this.btn_Serach = new Button() { Content = "查  询", Width = 100, Height = 30, FontSize = 15, FontWeight = FontWeights.Bold, Margin = new Thickness(2), HorizontalAlignment = HorizontalAlignment.Center, VerticalAlignment = VerticalAlignment.Top };
            this.btn_Serach.Click += new RoutedEventHandler(Button_Click);
            Utils.SetGridColumnSpan(btn_Serach, 5, 0, 2);
            _grid.Children.Add(btn_Serach);

            this._border = new Border() { Name = "main_Border", Width = 360, Height = 220, Child = _grid, CornerRadius = new CornerRadius(10), BorderThickness = new Thickness(1), BorderBrush = new SolidColorBrush(Colors.LightGray), Background = new SolidColorBrush(Color.FromArgb(255, 239, 239, 239)) };
            pnl_Top.Children.Add(_border);
            pnl_Main.Children.Add(pnl_Top);

            this.btn_Cancel = new Button() { Content = "关闭查询", Width = 100, Height = 25, Cursor = Cursors.Hand, HorizontalAlignment = HorizontalAlignment.Right };
            this.btn_Cancel.Click += new RoutedEventHandler(Button_Click);
            pnl_Bottom.Children.Add(btn_Cancel);
            pnl_Main.Children.Add(pnl_Bottom);
            this.Children.Add(pnl_Main);
        }

        /// <summary>
        /// 查询/取消按钮
        /// </summary>
        void Button_Click(object sender, RoutedEventArgs e)
        {
            bool b = false;
            if (sender.Equals(btn_Serach))
            {
                _parameter = "1=1";

                #region 验证获取条件
                if (this.txt_Name.Text.Trim().Length < 1 && this.txt_Phone.Text.Trim().Length < 1 && this.txt_Age0.Text.Trim().Length < 1 && this.txt_Age1.Text.Trim().Length < 1)
                {
                    MessageBox.Show("请输入查询条件", "警告", MessageBoxButton.OK);
                }
                else
                {
                    if (this.txt_Name.Text.Trim().Length > 0)
                    {
                        _parameter += "and XingM like '%" + this.txt_Name.Text.Trim() + "%'";
                    }
                    if (this.txt_Phone.Text.Trim().Length > 0)
                    {
                        _parameter += "and Phone like '%" + this.txt_Phone.Text.Trim() + "%'";
                    }
                    if (LoadData != null)
                        LoadData(this._parameter, null);
                    //if (this.txt_Age0.Text.Trim().Length > 0)
                    //{
                    //    _parameter += "and ";
                    //}
                    //if (this.txt_Age1.Text.Trim().Length > 0)
                    //{
                    //    _parameter += "and ";
                    //}
                }
                #endregion

                b = true;
            }

            this.Cartoon(.2, b);
        }

        /// <summary>
        /// 显示隐藏查询对话框
        /// </summary>
        /// <param name="t">隐藏显示时间</param>
        /// <param name="b">是否显示</param>
        private void Cartoon(double t, bool b)
        {
            this.btn_Cancel.Content = (this._place ? "显示查询" : "关闭查询");
            this.btn_Cancel.Click -= new RoutedEventHandler(Button_Click);
            this.btn_Cancel.MouseMove -= new MouseEventHandler(OnMouseMove);

            Point p = Utils.GetPoint(this.pnl_Main);
            Size s = Utils.GetSize(this.pnl_Top);
            p.Y += s.Height;
            TweenParameter _tween = new TweenParameter() { transition = TransitionType.easeInExpo, X = 0, Y = (this._place ? -p.Y : 0), ScaleX = 1, ScaleY = 1, time = t };
            Tweener.addTween(this.pnl_Main as FrameworkElement, _tween, new TweenEvent(cover_Cartoon), new object[] { b, "" });

            this._place = !this._place;
        }


        /// <summary>
        /// 鼠标移动，显示隐藏对话框
        /// </summary>
        private void OnMouseMove(object sender, MouseEventArgs e)
        {
            if (this._place) return;

            this.Cartoon(.2, false);
        }

        void cover_Cartoon(params object[] args)
        {
            if ((bool)args[0])
            {
                this.btn_Serach.IsEnabled = false;
                string u = "";
                //调用WCF服务

                this.btn_Serach.IsEnabled = true;
            }

            DispatcherTimer dt = new DispatcherTimer();
            dt.Interval = new TimeSpan(0, 0, 0, 0, 100);
            dt.Tick += new EventHandler(cover_Tick);
            dt.Start();
        }

        /// <summary>
        /// 对话框移动速度
        /// </summary>
        private void cover_Tick(object sender, EventArgs e)
        {
            ((DispatcherTimer)sender).Stop();
            this._canvas.Visibility = (this._place ? Visibility.Visible : Visibility.Collapsed);
            this.btn_Cancel.Click += new RoutedEventHandler(Button_Click);
            this.btn_Cancel.MouseMove += new MouseEventHandler(OnMouseMove);
        }

        /// <summary>
        /// 根据页面大小更改对话框的位置
        /// </summary>
        public void Update(FrameworkElement e)
        {
            Size s = new Size(e.ActualWidth,e.ActualHeight); //Utils.GetCurrentCanvasSize();
            Point p = new Point(s.Width/ 2,s.Height/ 2);

            Utils.SetSize(this._canvas, s);
            Utils.SetPoint(this._canvas,new Point());

            s = Utils.GetSize(this.pnl_Main);
            p.X -= s.Width/2;
            p.Y -= s.Height/2;
            Utils.SetPoint(this.pnl_Main, p);

            //if (this._place) return;
            //p = Utils.GetPoint(this.pnl_Main);
            //p.Y += s.Height - 25;
            //List<Transform> l = new List<Transform>();
            //l.Add(new TranslateTransform() { X = 0, Y = -p.Y });
            //this.pnl_Main.RenderTransform = Utils.Transform(l);
        }
    }
}
